clear all
set scheme s1mono

* Start log
log using "H:\Output\LGB\log_analysis.txt", replace


* Open dataset
use "H:\ProjData\LGB\main_data_rep.dta", clear


preserve

	collapse (mean) voted (semean) voted_semean=voted (count) voted_count=voted, by(lgb_indicator year)
	gen lower_ci = voted - 1.96 * voted_semean
	gen upper_ci = voted + 1.96 * voted_semean


	twoway	(line voted year if lgb_indicator==0, lcolor("200 200 250") lpattern(dash)) ///
			(line voted year if lgb_indicator==1, lcolor("250 200 200") lpattern(dash)) ///
			(line voted year if lgb_indicator==., lcolor("200 200 200") lpattern(dash)) ///
			(rcap upper_ci lower_ci year if lgb_indicator==0, lcolor(blue)) ///
			(rcap upper_ci lower_ci year if lgb_indicator==1, lcolor(red)) ///
			(rcap upper_ci lower_ci year if lgb_indicator==., lcolor(black)), ///
			legend(order(4 "Heterosexual control group" 5 "LGB group" 6 "Other")) title("Turnout by group and year") ///
			ylabel(0.7(0.05)1) xlabel(, valuelabel angle(vertical))
			
	graph export "H:\Output\LGB\fig1.pdf", replace

restore


preserve

	drop voted
	gen AREA = lc1000_lgb_indicator * 100
	rename AREA voted
	collapse (mean) voted (semean) voted_semean=voted (count) voted_count=voted, by(lgb_indicator year)
	gen lower_ci = voted - 1.96 * voted_semean
	gen upper_ci = voted + 1.96 * voted_semean

	twoway	(line voted year if lgb_indicator==0, lcolor("200 200 250") lpattern(dash)) ///
			(line voted year if lgb_indicator==1, lcolor("250 200 200") lpattern(dash)) ///
			(line voted year if lgb_indicator==., lcolor("200 200 200") lpattern(dash)) ///
			(rcap upper_ci lower_ci year if lgb_indicator==0, lcolor(blue)) ///
			(rcap upper_ci lower_ci year if lgb_indicator==1, lcolor(red)) ///
			(rcap upper_ci lower_ci year if lgb_indicator==., lcolor(black)), ///
			legend(order(4 "Heterosexual control group" 5 "LGB group" 6 "Other")) title("Context by group and year") ///
			ylabel(0.25(0.05)0.65) xlabel(, valuelabel angle(vertical))
			
	graph export "H:\Output\LGB\figA1.pdf", replace
	
restore



gen AREA = lc1000_lgb_indicator * 100
label variable AREA "LGB context"


* Main results
reghdfe voted AREA if lgb_indicator==0, noabsorb cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA1.tex", replace keep(AREA) ctitle(MC) addtext(Ind. FE, NO, Year FE, NO) label tex(frag)
reghdfe voted AREA if lgb_indicator==1, noabsorb cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA1.tex", append keep(AREA) ctitle(LGB) addtext(Ind. FE, NO, Year FE, NO) label tex(frag)

reghdfe voted AREA i.year if lgb_indicator==0, absorb(LopNr) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA1.tex", append keep(AREA) ctitle(MC) addtext(Ind. FE, YES, Year FE, YES) label tex(frag)
reghdfe voted AREA i.year if lgb_indicator==1, absorb(LopNr) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA1.tex", append keep(AREA) ctitle(LGB) addtext(Ind. FE, YES, Year FE, YES) label tex(frag)

reghdfe voted c.AREA##c.lgb_indicator, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA1.tex", append keep(AREA c.AREA#c.lgb_indicator) ctitle(ALL) addtext(Ind. FE, YES, Year FE, YES) label tex(frag)


* Main graph
matrix V=e(V)
scalar beta_AREA = _b[AREA]
scalar beta_interaction = _b[c.AREA#c.lgb_indicator]
scalar beta_combined = beta_AREA + beta_interaction

scalar se_AREA = _se[AREA]
scalar se_interaction = _se[c.AREA#c.lgb_indicator]
scalar cov_interaction = V[1,3]
scalar se_combined = sqrt(se_AREA^2 + se_interaction^2 + 2*cov_interaction)

scalar ci_lower_0 = beta_AREA -1.96*se_AREA
scalar ci_upper_0 = beta_AREA +1.96*se_AREA
scalar ci_lower_1 = beta_combined -1.96 * se_combined
scalar ci_upper_1 = beta_combined +1.96 * se_combined

gen GROUP = .
replace GROUP = 0 in 1
replace GROUP = 1 in 2
gen BETA = .
replace BETA = beta_AREA in 1
replace BETA = beta_combined in 2
gen LOWER = .
replace LOWER = ci_lower_0 in 1
replace LOWER = ci_lower_1 in 2
gen UPPER = .
replace UPPER = ci_upper_0 in 1
replace UPPER = ci_upper_1 in 2

gen y_line1 = .0075771
replace y_line1 = . if _n>2

gen GROUP0 = GROUP
replace GROUP0 = 0 in 2
gen y_line2 = .
replace y_line2 = .0075771 in 1
replace y_line2 = -.0079897 in 2


twoway (line BETA GROUP, lpattern(solid) lcolor(black)) ///
		(line y_line1 GROUP, lcolor(gs10) lpattern(dash)) ///
		(line y_line2 GROUP0, lcolor(red) lpattern(dash)) ///
		(rcap UPPER LOWER GROUP) ///
		(scatter BETA GROUP, mcolor(black) msymbol(diamond)), ///
		xlabel(0 "HC" 1 "LGB") legend(off) ///
		xscale(range(-0.25 1.25)) ///
		ytitle("DiD estimate") ///
		text(0.01 0 "DiDiD:", place(north)) ///
		text(0.0085 0 "0.0156***", place(north) color(red)) ///
		xtitle("")
		
graph export "H:\Output\LGB\fig2.pdf", replace






* Robustness checks
replace AREA = lc5000_lgb_indicator * 100
reghdfe voted c.AREA##c.lgb_indicator, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA2.tex", replace keep(AREA c.AREA#c.lgb_indicator) ctitle(ALL) addtext(Ind. FE, YES, Year FE, YES, Area size, 5000, Contextual control, NO, Buffer, NO) label tex(frag)
replace AREA = lc10000_lgb_indicator * 100
reghdfe voted c.AREA##c.lgb_indicator, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA2.tex", append keep(AREA c.AREA#c.lgb_indicator) ctitle(ALL) addtext(Ind. FE, YES, Year FE, YES, Area size, 10000, Contextual control, NO, Buffer, NO) label tex(frag)
replace AREA = lc1000_lgb_indicator * 100
reghdfe voted c.AREA##c.lgb_indicator c.lc1000_edu##c.lgb_indicator, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA2.tex", append keep(AREA c.AREA#c.lgb_indicator) ctitle(ALL) addtext(Ind. FE, YES, Year FE, YES, Area size, 1000, Contextual control, EDUCATION, Buffer, NO) label tex(frag)
reghdfe voted c.AREA##c.lgb_indicator c.lc1000_inc##c.lgb_indicator, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA2.tex", append keep(AREA c.AREA#c.lgb_indicator) ctitle(ALL) addtext(Ind. FE, YES, Year FE, YES, Area size, 1000, Contextual control, INCOME, Buffer, NO) label tex(frag)
replace AREA = ex_lc1000_lgb_indicator * 100
reghdfe voted c.AREA##c.lgb_indicator, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA2.tex", append keep(AREA c.AREA#c.lgb_indicator) ctitle(ALL) addtext(Ind. FE, YES, Year FE, YES, Area size, 1000, Contextual control, NO, Buffer, YES) label tex(frag)
replace AREA = lc1000_lgb_indicator * 100
bysort LopNr: egen mean_voted = mean(voted)
reghdfe voted c.AREA##c.lgb_indicator if mean_voted!=1, absorb(LopNr year lgb_indicator#year) cluster(LopNr)
outreg2 using "H:\Output\LGB\tableA2.tex", append keep(AREA c.AREA#c.lgb_indicator) ctitle(SV) addtext(Ind. FE, YES, Year FE, YES, Area size, 1000, Contextual control, NO, Buffer, NO) label tex(frag)


* Descriptives
capture gen lc1000 = lc1000_lgb_indicator * 100
capture gen lc5000 = lc5000_lgb_indicator * 100
capture gen lc10000= lc10000_lgb_indicator * 100
capture gen turnout = voted * 100

file open table using "H:\Output\LGB\tableA3.tex", write replace
file write table "\begin{table}[H]" _n
file write table "\begin{tabular}{ll|ccc|ccc|ccc}" _n
file write table " & & \multicolumn{3}{c}{LGB} & \multicolumn{3}{c}{HC} & \multicolumn{3}{c}{Other} \\" _n
file write table "Variable & Year & Mean & SD & N & Mean & SD & N & Mean & SD & N \\ \hline" _n
foreach v in turnout lc1000 lc5000 lc10000 lc1000_edu lc1000_inc {

	file write table "`v' \\" _n

	foreach y in 1994 2010 2018 2022 {
		file write table " & `y' &"
		su `v' if lgb_indicator == 1 & year == `y'
		local m : di %12.3f r(mean)
		local sd : di %12.3f r(sd)
		local n = r(N)
		if "`v'" != "lc1000_edu" & "`v'" != "lc1000_inc" {
			local m = "`m'\%"
			local sd = "`sd'\%"
		}
		file write table "`m' & `sd' & `n' & "
		
		su `v' if lgb_indicator == 0 & year == `y'
		local m : di %12.3f r(mean)
		local sd : di %12.3f r(sd)
		local n = r(N)
		if "`v'" != "lc1000_edu" & "`v'" != "lc1000_inc" {
			local m = "`m'\%"
			local sd = "`sd'\%"
		}
		file write table "`m' & `sd' & `n' & "
		
		su `v' if lgb_indicator == . & year == `y'
		local m : di %12.4f r(mean)
		local sd : di %12.3f r(sd)
		local n = r(N)
		if "`v'" != "lc1000_edu" & "`v'" != "lc1000_inc" {
			local m = "`m'\%"
			local sd = "`sd'\%"
		}
		file write table "`m' & `sd' & `n' \\" _n
	}
}
file write table "\hline" _n "\end{tabular}" _n "\end{table}"
file close table




* Table for population densities
gen density = 10^6 * lc1000_lgb_indicator_n / (3.14159 * lc1000_lgb_indicator_dist^2)

file open table using "H:\Output\LGB\tableA4.tex", write replace
file write table "\begin{table}" _n
file write table "\begin{tabular}{l|ccccc}" _n

file write table " & \multicolumn{5}{c}{LGB} \\" _n
file write table "Year & Mean & SD & Min & Max & N \\ \hline" _n
foreach y in 1994 2010 2018 2022 {
	su density if lgb_indicator == 1 & year == `y'
	local m : di round(r(mean))
	local sd : di round(r(sd))
	local min : di %12.3f r(min)
	local max : di round(r(max))
	local n = r(N)
	file write table "`y' & `m' & `sd' & `min' & `max' & `n' \\ " _n
}
file write table " & & & & & \\" _n

file write table " & \multicolumn{5}{c}{Heterosexual controls} \\" _n
file write table "Year & Mean & SD & Min & Max & N \\ \hline" _n
foreach y in 1994 2010 2018 2022 {
	su density if lgb_indicator == 0 & year == `y'
	local m : di round(r(mean))
	local sd : di round(r(sd))
	local min : di %12.3f r(min)
	local max : di round(r(max))
	local n = r(N)
	file write table "`y' & `m' & `sd' & `min' & `max' & `n' \\" _n
}
file write table " & & & & & \\" _n

file write table " & \multicolumn{5}{c}{Others} \\" _n
file write table "Year & Mean & SD & Min & Max & N \\ \hline" _n
foreach y in 1994 2010 2018 2022 {
	su density if lgb_indicator == . & year == `y'
	local m : di round(r(mean))
	local sd : di round(r(sd))
	local min : di %12.3f r(min)
	local max : di round(r(max))
	local n = r(N)
	file write table "`y' & `m' & `sd' & `min' & `max' & `n' \\" _n
}
file write table "\hline" _n "\end{tabular}" _n "\end{table}"
file close table


log close
